package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.OrganizeCustom;
import com.turing.post.salesmanage.util.OrganizeCustomDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "salesmanage.impl.OrganizeCustomDaoImpl")
@Remote
@Local
public class OrganizeCustomDaoImpl implements OrganizeCustomDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<OrganizeCustom> getOrganizeCustomByFullName(String fullName) {
		String jpql = "select * from OrganizeCustom p where p.fullname like ?1";
		Query query = _em.createNativeQuery(jpql, OrganizeCustom.class);
		if(PCPSUtil.isEmpty(fullName)){
			fullName="";
		}
		query.setParameter(1, "%"+fullName+"%");
		return query.getResultList();
	}

	@Override
	public OrganizeCustom getOrganizeCustomByOrganizeId(BigInteger organizeId) {
		return _em.find(OrganizeCustom.class, organizeId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrganizeCustom> getOrganizeCustomList(PCPSWebInput webInput,
			Integer page, String fullName, String telephone) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select * from OrganizeCustom t where t.issuecompanyno='"
					+ issueCompanyNo + "'";
		} else {
			sql = "select * from OrganizeCustom t where 1=1";
		}
		// 单位名称
		if (!PCPSUtil.isEmpty(fullName)) {
			sql = sql + " and t.FullName like '%" + fullName + "%'";
		}
		// 电话
		if (!PCPSUtil.isEmpty(telephone)) {
			sql = sql + " and t.telephone like '%" + telephone + "%'";
		}
		sql = sql + " order by t.createtime";
		Query query = _em.createNativeQuery(sql, OrganizeCustom.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getOrganizeCustomTotal(PCPSWebInput webInput,
			String fullName, String telephone) {
		String sql = "";
		//平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select nvl(count(*),0) from OrganizeCustom t where t.issuecompanyno='"
					+ issueCompanyNo + "'";
		} else {
			sql = "select nvl(count(*),0) from OrganizeCustom t where 1=1";
		}
		// 单位名称
		if (!PCPSUtil.isEmpty(fullName)) {
			sql = sql + " and t.FullName like '%" + fullName + "%'";
		}
		// 电话
		if (!PCPSUtil.isEmpty(telephone)) {
			sql = sql + " and t.telephone like '%" + telephone + "%'";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertOrganizeCustom(OrganizeCustom organizeCustom)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, organizeCustom);
	}

	@Override
	public void updateOrganizeCustom(OrganizeCustom organizeCustom)
			throws PCPSApplicationException {
		GeneralDao.update(_em, organizeCustom);
	}
}
